Header Ads Widget

Home Automation with Telegram and ESP32

 The ESP32 is a great platform for home automation projects. There are many ways to control ESP32-based devices using Telegram. In this blog, we'll explore how to use Telegram to control ESP32-based devices.


Home Automation with Telegram

Telegram is a great platform for controlling devices remotely. It's simple to set up and use, and it's free. You can use Telegram to control ESP32-based devices in a variety of ways. For example, you can use Telegram to turn on and off devices or to control them using voice commands.

There are a few things you'll need to set up before you can start using Telegram to control your devices. First, you'll need to set up a Telegram account and download the Telegram app. Next, you'll need to create a Telegram bot. This is a simple process that only takes a few minutes.

Once you've created your Telegram bot, you can start controlling your ESP32-based devices using Telegram. In this blog, we'll explore some ways you can control your devices using Telegram. We'll also provide some tips and tricks to help you get the most out of your Telegram-controlled home automation system.

this link will help to how to create Telegram bot How to create telegram bot or follow the below video.



Home Automation with Telegram making Video:



Home Automation with Telegram code:

#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>   // Universal Telegram Bot Library written by Brian Lough: https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot
#include <ArduinoJson.h>

// Replace with your network credentials
const char* ssid = "mewt";
const char* password = "Veera@0000";

// Initialize Telegram BOT
#define BOTtoken "5373508960:AAGyVEL-u6FPtRQn_bgm7t5sVUCJBr13d_E"  // your Bot Token (Get from Botfather)

// Use @myidbot to find out the chat ID of an individual or a group
// Also note that you need to click "start" on a bot before it can
// message you
#define CHAT_ID "6935...."

#ifdef ESP8266
  X509List cert(TELEGRAM_CERTIFICATE_ROOT);
#endif

WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);

// Checks for new messages every 1 second.
int botRequestDelay = 1000;
unsigned long lastTimeBotRan;

const int ledPin = 4;
bool ledState = HIGH;

// Handle what happens when you receive new messages
void handleNewMessages(int numNewMessages) {
  Serial.println("handleNewMessages");
  Serial.println(String(numNewMessages));

  for (int i=0; i<numNewMessages; i++) {
    // Chat id of the requester
    String chat_id = String(bot.messages[i].chat_id);
    if (chat_id != CHAT_ID){
      bot.sendMessage(chat_id, "Unauthorized user", "");
      continue;
    }
    
    // Print the received message
    String text = bot.messages[i].text;
    Serial.println(text);

    String from_name = bot.messages[i].from_name;

    if (text == "/start") {
      String welcome = "Welcome, " + from_name + ".\n";
      welcome += "meworkstelugu\n\n";
      welcome += "Use the following commands to control your outputs.\n\n";
      welcome += "/light_on to turn GPIO ON \n";
      welcome += "/light_off to turn GPIO OFF \n";
      welcome += "/state to request current GPIO state \n";
      bot.sendMessage(chat_id, welcome, "");
    }

    if (text == "/light_on") {
      bot.sendMessage(chat_id, "LIGHT state set to ON", "");
      ledState = LOW;
      digitalWrite(ledPin, ledState);
    }
    
    if (text == "/light_off") {
      bot.sendMessage(chat_id, "LIGHT state set to OFF", "");
      ledState = HIGH;
      digitalWrite(ledPin, ledState);
    }
    
    if (text == "/state") {
      if (digitalRead(ledPin)){
        bot.sendMessage(chat_id, "LIGHT is OFF", "");
      }
      else{
        bot.sendMessage(chat_id, "LIGHT is ON", "");
      }
    }
  }
}

void setup() {
  Serial.begin(115200);

  #ifdef ESP8266
    configTime(0, 0, "pool.ntp.org");      // get UTC time via NTP
    client.setTrustAnchors(&cert); // Add root certificate for api.telegram.org
  #endif

  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, ledState);
  
  // Connect to Wi-Fi
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  #ifdef ESP32
    client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
  #endif
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }
  // Print ESP32 Local IP Address
  Serial.println(WiFi.localIP());
}

void loop() {
  if (millis() > lastTimeBotRan + botRequestDelay)  {
    int numNewMessages = bot.getUpdates(bot.last_message_received + 1);

    while(numNewMessages) {
      Serial.println("got response");
      handleNewMessages(numNewMessages);
      numNewMessages = bot.getUpdates(bot.last_message_received + 1);
    }
    lastTimeBotRan = millis();
  }
}





Post a Comment

0 Comments